﻿using System;
using System.Diagnostics;

namespace CSharpLogger.Product
{
    class EventViewerLogger : ILoggerHandler
    {
        #region [ Fields ]
        private static readonly EventViewerLogger _log2EV = new EventViewerLogger();
        private Level _level;
        #endregion

        #region [ Properties ]
        public static EventViewerLogger Log2EV
        {
            get { return _log2EV; }
        }

        public string AppName { get; set; }

        public string UserName { get; set; }

        public string Message { get; set; }

        public string Host { get; set; }

        public Level Level { get; set; } 
        #endregion

        #region [ Constructor ]
        private EventViewerLogger() { }
        #endregion

        /// <summary>
        /// Log the information in the Event Log Registry if the Level is setted to ERROR, FATAL or INFO
        /// </summary>
        public void Log()
        {
            if (Level == CSharpLogger.Level.ERROR || Level == CSharpLogger.Level.FATAL || Level == CSharpLogger.Level.INFO)
            {
                EventLogEntryType entry = 0;

                if (Level == CSharpLogger.Level.ERROR || Level == CSharpLogger.Level.FATAL)
                    entry = EventLogEntryType.Error;
                else if (Level == CSharpLogger.Level.WARN)
                    entry = EventLogEntryType.Warning;
                else if (Level == CSharpLogger.Level.INFO)
                    entry = EventLogEntryType.Information;

                if (!EventLog.SourceExists(_log2EV.AppName))
                    EventLog.CreateEventSource(_log2EV.AppName, _log2EV.AppName);
                                   
                    EventLog.WriteEntry(_log2EV.AppName, _log2EV.Message, entry);
            }
        }    
        
    }
}
